package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/models"
)

type SuggestionController struct {
	//Dependent services
}

func NewSuggestionController() *SuggestionController {
	return &SuggestionController{
		//Inject services
	}
}

func (r *SuggestionController) Index(ctx http.Context) http.Response {
	sgs := []models.Suggestion{}
	queries := ctx.Request().Queries()
	pagination, _ := httpfacade.NewResult(ctx).SearchByParams(queries, map[string]interface{}{}).ResultPagination(&sgs)
	return pagination
}

func (r *SuggestionController) Show(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	sg := models.Suggestion{}
	facades.Orm().Query().Model(&models.Suggestion{}).Where("id=?", id).Find(&sg)
	return httpfacade.NewResult(ctx).Success("", sg)
}

func (r *SuggestionController) Store(ctx http.Context) http.Response {

	content := ctx.Request().Input("content")
	if content == "" {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据不为空", nil)
	}
	facades.Orm().Query().Model(&models.Suggestion{}).Create(&models.Suggestion{
		Content: content,
	})

	return httpfacade.NewResult(ctx).Success("感谢反馈", nil)
}

func (r *SuggestionController) Update(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	content := ctx.Request().Input("content")
	if content == "" {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "数据不为空", nil)
	}
	facades.Orm().Query().Model(&models.Suggestion{}).Where("id=?", id).Update("content", content)
	return httpfacade.NewResult(ctx).Success("修改成功", nil)
}

func (r *SuggestionController) Destroy(ctx http.Context) http.Response {
	id := ctx.Request().Route("id")
	sg := models.Suggestion{}
	facades.Orm().Query().Model(&models.Suggestion{}).Where("id=?", id).Find(&sg)
	facades.Orm().Query().Model(&models.Suggestion{}).Where("id=?", id).Delete(&sg)
	return httpfacade.NewResult(ctx).Success("删除成功", nil)
}
